/**
 * 
 */
package ca.utoronto.cms.cscc85.project4.teamrf;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 *This activity searchs through the recipes
 */
public class SearchRecipesActivity extends ListActivity implements OnItemClickListener, TextWatcher {
	ListView resultsList;
	String[] columns = { "name", "time", "_id"};
	EditText searchText;
	Cursor results;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	// set up the view
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.searchrecipes);
        
        // set up the listview
        resultsList = this.getListView();
        resultsList.setAdapter(new ArrayAdapter<String>(this, R.layout.resultentry, columns));
        resultsList.setTextFilterEnabled(true);
        resultsList.setOnItemClickListener(this);
        
        // set up the textview
        searchText = (EditText) findViewById(R.id.searchText);
        searchText.addTextChangedListener(this);
    	
    	// Populate the listview

        String query = "SELECT * FROM recipes WHERE name LIKE'" + searchText.getText() + "%' ORDER BY name ASC";
		results = Main.getDatabase().rawQuery(query, null);
		
		startManagingCursor(results);


	    int[] to = new int[] { R.id.nameText, R.id.prepText, R.id.idLabel };
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.resultentry, results, columns, to);
        this.setListAdapter(cursorAdapter);

    }

    /**
     * The recipe the user clicks on will be selected and sent ot hte view activity
     */
	public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // point cursor to the selected item
		results.moveToPosition(position);
		
		// create intent and pass bundle
		Intent viewIntent = new Intent(getApplicationContext(), ViewRecipeActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("name", results.getString(1));
		bundle.putString("ingredients", results.getString(2));
		bundle.putString("directions", results.getString(3));
		bundle.putInt("time", results.getInt(4));
		viewIntent.putExtras(bundle);
		startActivity(viewIntent);
		
    }

	/**
	 * does the query for hte letters typed
	 */
	public void afterTextChanged(Editable arg0) {
		String query;
    	
    	// start the query
        query = "SELECT * FROM recipes WHERE name LIKE'" + searchText.getText() + "%'";
        Log.d("SEARCH", "Query: " + query);
		results = Main.getDatabase().rawQuery(query, null);
		Log.d("SEARCH", "Results: " + results.getCount());
		
		resultsList.setAdapter(new ArrayAdapter<String>(this, R.layout.resultentry, columns));
        startManagingCursor(results);
        
        int[] to = new int[] { R.id.nameText, R.id.prepText, R.id.idLabel };
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.resultentry, results, columns, to);
        this.setListAdapter(cursorAdapter);
	}
	
	
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// do nothing
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// do nothing
	}

}
